package com.ztgf.sys.api.cms;

import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.Md5Utils;
import com.ztgf.common.utils.RandomValidateCodeUtil;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.sys.models.vo.SysUserVO;
import com.ztgf.user.models.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 登陆
 *
 * @author YUE
 * @create 2020-05-27 17:32
 */
@RestController
@RequestMapping("/cms/sysLogin")
@Slf4j
@Api(value = "SysLoginController",tags = "登陆")
public class SysLoginController {

    @DubboReference
    private ISysUserService sysUserService;
    @DubboReference
    private IUserLoginService userLoginService;

    @ApiOperation(value = "退出登陆",notes = "退出登陆")
    @PostMapping(value = "/quiteLogin")
    public Result quiteLogin(){
        sysUserService.quietLogin();
        return new Result();
    }

    @ApiOperation(value = "登陆",notes = "登陆")
    @PostMapping(value = "/login")
    public Result<SysUserVO> login(String account, String password) {
        Result sysUserResult = sysUserService.verifyLoginInfo(account, password, CommonParameter.getHeadParamsBO().getClientIp());
        if(sysUserResult.getCode() != CodeEnum.SUCCESS.getCode()){
            return sysUserResult;
        }
        SysUserDTO sysUserDTO = (SysUserDTO) sysUserResult.getData();
        SysUserVO sysUserVO = new SysUserVO();

        sysUserService.resetSession(sysUserDTO.getId());

        String sessionId = Md5Utils.encryption(sysUserDTO.getId() + "" + System.currentTimeMillis());
        BeanUtils.copyProperties(sysUserDTO,sysUserVO);
        sysUserVO.setSessionId(sessionId);
        sysUserResult.setData(sysUserVO);
        //保存session进redis
        sysUserService.setUserBySessionId(sysUserDTO, sessionId);
        return sysUserResult;
    }

    @ApiOperation(value = "手机验证码登陆",notes = "手机验证码登陆")
    @PostMapping(value = "/loginMobile")
    public Result<SysUserVO> loginMobile(String mobile, String captcha) {

        boolean flag_sms = userLoginService.getSmsCaptcha(mobile, captcha, 1);
        if (!flag_sms){
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        Result sysUserResult = sysUserService.verifyLoginInfoByMobile(mobile,CommonParameter.getHeadParamsBO().getClientIp());
        if(sysUserResult.getCode() != CodeEnum.SUCCESS.getCode()){
            return sysUserResult;
        }
        SysUserDTO sysUserDTO = (SysUserDTO) sysUserResult.getData();
        SysUserVO sysUserVO = new SysUserVO();
        sysUserService.resetSession(sysUserDTO.getId());
        String sessionId = Md5Utils.encryption(sysUserDTO.getId() + "" + System.currentTimeMillis());
        BeanUtils.copyProperties(sysUserDTO,sysUserVO);
        sysUserVO.setSessionId(sessionId);
        sysUserResult.setData(sysUserVO);
        //保存session进redis
        sysUserService.setUserBySessionId(sysUserDTO, sessionId);

        return sysUserResult;
    }

    @ApiOperation(value = "单点登录",notes = "单点登录")
    @PostMapping(value = "/loginSSO")
    public Result<SysUserVO> loginSSO(String mobile, String captcha) {
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(captcha)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Result<SysUserVO> result = sysUserService.saveLoginSSO(mobile, captcha, CommonParameter.getHeadParamsBO().getClientIp());

        return result;
    }

    @ApiOperation(value = "单点登录发送短信",notes = "单点登录发送短信")
    @PostMapping(value = "/sendSms")
    public Result sendSms(String mobile, String captcha, String randomStr) {
        if (StringUtils.isEmpty(mobile)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        String saveCode = userLoginService.getVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + randomStr);

        if (StringUtils.isEmpty(saveCode)) {
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }
        //误差在10px以内
        Integer saveCodeInt1 = Integer.parseInt(saveCode) - 10;
        Integer saveCodeInt2 = Integer.parseInt(saveCode) + 10;
        Integer codeInt = Integer.parseInt(captcha);

        if (saveCodeInt1 <= codeInt && saveCodeInt2 >= codeInt) {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + randomStr);
        } else {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + randomStr);
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }

        return sysUserService.getCaptcha(mobile);
    }

    @ApiOperation(value = "单点登录验证token",notes = "单点登录验证token")
    @PostMapping(value = "/validateToken")
    public Result validateToken(String token) {
        if (StringUtils.isEmpty(token)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        return sysUserService.validateToken(token, CommonParameter.getHeadParamsBO().getClientIp());
    }

    @ApiOperation(value = "退出sso",notes = "退出sso")
    @PostMapping(value = "/quiteSSO")
    public Result quiteSSO(String token){
        sysUserService.quiteSSO(token);
        return new Result();
    }


    @ApiOperation(value = "单点登录验证Code",notes = "单点登录验证Code")
    @PostMapping(value = "/validateCode")
    public Result<SysUserVO> validateCode(String code) {
        if (StringUtils.isEmpty(code)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        return sysUserService.validateCode(code, CommonParameter.getHeadParamsBO().getClientIp());
    }

}
